package com.xulinhao.edu.controller;


import com.xulinhao.commonutils.R;
import com.xulinhao.edu.client.VodClient;
import com.xulinhao.edu.entity.Video;
import com.xulinhao.edu.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author xulinhao
 * @since 2021-08-20
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class VideoController {
    @Autowired
    private VideoService videoService;

    //注入VodClient
    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody Video video){
        videoService.save(video);
        return R.ok();
    }
    //删除小节
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        //根据小节id获取视频id,调用方法实现视频删除
        Video video = videoService.getById(id);
        String videoSourceId = video.getVideoSourceId();
        if (!StringUtils.isEmpty(videoSourceId)) {
            vodClient.removeVideo(videoSourceId);
        }
        //删除小节
        videoService.removeById(id);
        return R.ok();
    }

    //修改小节
    //根据章节id查询
    @GetMapping("getVideoInfo/{videoId}")
    public R getVideoInfo(@PathVariable String videoId){
        Video video = videoService.getById(videoId);
        return R.ok().data("video", video);
    }

    //修改章节
    @PostMapping("updateVideo")
    public R updateChapter(@RequestBody Video video){
        videoService.updateById(video);
        return R.ok();
    }

}

